package com.oct.website.controller;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.oct.website.entity.Avgthi;
import com.oct.website.entity.Scenic;
import com.oct.website.mapper.AvgThiMapper;
import com.oct.website.mapper.ScenicMapper;
import com.oct.website.mapper.ThiDataMapper;

@Controller
@RequestMapping("/scenicInfo")
public class ScenicInfoController {

	Logger logger = Logger.getLogger("ScenicInfoController.java");
	@Autowired
	private HttpServletRequest request; //自动注入request
	

	@Autowired
	private AvgThiMapper avgThiMapper;
	
	@Autowired
	private ScenicMapper scenicMapper;
	
	@Autowired
	private ThiDataMapper thiDataMapper;
	
	/**
	 * 进入景区详情页面
	 */
	@RequestMapping("/{scenicName}")
	public ModelAndView toScenic(@PathVariable("scenicName") String scenicName) {
		

		Scenic scenic = this.scenicMapper.selectScenic(scenicName);
		logger.info(scenicName + "; scenic景区信息:"+ JSON.toJSONString(scenic));
		
		// 获取最新的指数数据
		BigDecimal thi = this.thiDataMapper.queryThiNewValue(scenicName);
		ModelAndView mv = new ModelAndView("/scenic/scenic-info");
		
		// 获取统计信息（前12月份和相关指数信息）
		List<Map> staList = this.thiDataMapper.queryThiSta(scenicName);
		logger.info("统计信息："+JSONArray.toJSONString(staList));
		List<Map> sortedEmp = staList.stream().sorted((i, j) -> Integer.parseInt(i.get("time1").toString()) - Integer.parseInt(j.get("time1").toString())).collect(Collectors.toList());
		logger.info("排序后统计信息："+JSONArray.toJSONString(sortedEmp));
		
		// 获取月份数组
		List<String> monthList = sortedEmp.stream().map(e -> e.get("time1").toString()).collect(Collectors.toList());
		// 获取thi值数组
		List<BigDecimal> thiList = sortedEmp.stream().map(e -> new BigDecimal(e.get("thi_index").toString())).collect(Collectors.toList());
		
		mv.addObject("scenic", scenic);
		mv.addObject("thi", thi);
		mv.addObject("monthList", monthList);
		mv.addObject("thiList", thiList);
		
		return mv;
	}
	
}

